package com.juejin.www.dao;

import com.juejin.www.model.DO.Like;
import com.juejin.www.model.DTO.LikeDTO;
import org.apache.ibatis.annotations.*;

/**
 * @author Jacky
 */
@Mapper
public interface LikeDao {
    /**
     * 点赞
     * @param like 点赞实体
     */
    @Insert("insert into likes(id, be_liked_id, be_liked_type, user_id, create_time, update_time) " +
            "values(null,#{beLikedId},#{beLikedType},#{userId},#{createTime},#{updateTime})")
    void add(Like like);

    /**
     * 添加文章点赞数
     * @param id 文章id
     */
    @Update("update articles set like_count = like_count+1 where id = #{id}")
    void addArticleLikeCount(Integer id);

    /**
     * 添加沸点点赞数
     * @param id 沸点id
     */
    @Update("update boiling_points set like_count = like_count+1 where id=#{id}")
    void addBoilingPointLikeCount(Integer id);

    /**
     * 添加评论点赞数
     * @param id 评论id
     */
    @Update("update comments set like_count = like_count+1 where id=#{id}")
    void addCommentLikeCount(Integer id);

    /**
     * 取消点赞
     * @param likeDTO 点赞实体
     */
    @Delete("delete from likes where be_liked_id=#{beLikedId} and be_liked_type=#{beLikedType} and user_id=#{userId}")
    void cancelLike(LikeDTO likeDTO);

    /**
     * 查看用户是否点赞
     * @param beLikedId 被点赞id
     * @param beLikedType 被点赞类型
     * @param userId 用户id
     * @return 查询结果条数
     */
    @Select("select count(*) from likes where be_liked_id=#{beLikedId} and be_liked_type=#{beLikedType} and user_id=#{userId}")
    Integer checkLike(Integer beLikedId, Integer beLikedType, Integer userId);

    /**
     * 减少文章点赞数
     * @param id 文章id
     */
    @Update("update articles set like_count = like_count-1 where id=#{id}")
    void reduceArticleLikeCount(Integer id);

    /**
     * 减少沸点点赞数
     * @param id 沸点id
     */
    @Update("update boiling_points set like_count = like_count-1 where id=#{id}")
    void reduceBoilingPointLikeCount(Integer id);

    /**
     * 减少评论点赞数
     * @param id 评论id
     */
    @Update("update comments set like_count = like_count-1 where id=#{id}")
    void reduceCommentLikeCount(Integer id);
}
